"use strict";
/**
 * @class  elFinder command "mkdir"
 * Create new folder
 *
 * @author Dmitry (dio) Levashov
 **/
elFinder.prototype.commands.mkdir = function() {
	this.disableOnSearch = true;
	this.updateOnSelect  = false;
	this.mime            = 'directory';
	this.prefix          = 'untitled folder';
/* 	this.exec            = $.proxy(this.fm.res('mixin', 'make'), this); */

/* 	this.dialog_for_exec = function () {
		var str = this.fm.i18n('mk_album_title');
		var label1 = $('<label></label>').css({display: "block",fontWeight: "bold",padding: "0 0 2px"}).html(this.fm.i18n('album_title'));
		var label2 = $('<label></label>').css({display: "block",fontWeight: "bold",padding: "0 0 2px"}).html(this.fm.i18n('album_content'));
		var input = $('<input type="text"/>').addClass('text ui-widget-content ui-corner-all');
		var textarea = $('<textarea></textarea').addClass('text ui-widget-content ui-corner-all');
		var argus = arguments;
		$('<div title=' + str + ' class=dialogAlbum></div>').dialog({
			modal	:	true,
			close	:	function (){this.dfrd.reject()},
			buttons	:	{
				'ok'	:	function(argus){
								console.log(arguments);
								var dfrd = this.dfrd;
								console.log(dfrd);
								input_value = $.trim($(this).find('input').val());

								node.find('.elfinder-cwd-filename').empty('').append(input.val(file.name));
								parent = input.parent();

								if (parent.length) {
									if (!input_value) {
										return dfrd.reject('errInvName');
									}
									if (fm.fileByName(input_value, phash)) {
										return dfrd.reject(['errExists', name]);
									}

									parent.html(fm.escape(name));

									fm.lockfiles({files : [id]});
								}
							},
				'cancel'	:	function (){this.dfrd.reject()}
			}
		}).append(label1)
			.append(input)
			.append(label2)
			.append(textarea);
	}, */

	this.exec = function() {
			var fm   = this.fm,
			cmd  = this.name,
			cwd  = fm.getUI('cwd'),
			dfrd = $.Deferred()
				.fail(function(error) {
					cwd.trigger('unselectall');
					error && fm.error(error);
				})
				.always(function() {
					// input.remove();
					$('.dialogAlbum').remove();
					node.remove();
					fm.enable();
				}),//end of dfrd defined
			id    = 'tmp_'+parseInt(Math.random()*100000),
			phash = fm.cwd().hash,
			date = new Date(),
			file   = {
				hash  : id,
				name  : fm.uniqueName(this.prefix),
				mime  : this.mime,
				read  : true,
				write : true,
				date  : 'Today '+date.getHours()+':'+date.getMinutes()
			},
			node = cwd.trigger('create.'+fm.namespace, file).find('#'+id);//end of var defined
			
		$('<div title=' + fm.i18n('mk_album_title') + ' class=dialogAlbum></div>').dialog({
			modal	:	true,
			close	:	function (){dfrd.reject();$(this).dialog( "destroy" );},
			buttons	:	{
				'ok'	:	function(){
								console.log(arguments);
								console.log(dfrd);
								var input_value = fm.escape($.trim($(this).find('input').val()));
								var textarea	=	fm.escape($.trim($(this).find('textarea').val()))
								if (!input_value) {
									return dfrd.reject('errInvName');
								}
								if (fm.fileByName(input_value, phash)) {
									return dfrd.reject(['errExists', input_value]);
								}
							node.find('.elfinder-cwd-filename').html(input_value);
							fm.lockfiles({files : [id]});
							///////////////////SYNC REQUEST///////////////////////////	
								fm.request({
								data        : {cmd : cmd, name : input_value,content : textarea, target : phash},
								notify      : {type : cmd, cnt : 1},
								preventFail : true,
								syncOnFail  : true
							})
							.fail(function(error) {
								dfrd.reject(error);
							})
							.done(function(data) {
								dfrd.resolve(data);
							});
							////////////////after async close dialog////////////////////////////
							$(this).dialog( "destroy" );
							},
				'cancel'	:	function (){this.dfrd.reject();$(this).dialog( "destroy" );}
			}
		}).append($('<label></label>').html(fm.i18n('album_title')))
			.append($('<input type="text"/>').addClass('text ui-widget-content ui-corner-all'))
			.append($('<label></label>').html(fm.i18n('album_content')))
			.append($('<textarea></textarea').addClass('text ui-widget-content ui-corner-all'));

		if (this.disabled() || !node.length) {
			return dfrd.reject();
		}

		fm.disable();
		// node.find('.elfinder-cwd-filename').empty('').append(input.val(file.name));
		//input.select().focus();

		return dfrd;

	}

	this.cancel = function() {
		alert("keyboard esc pushed");
	}

	this.getstate = function() {
		return !this._disabled && this.fm.cwd().write ? 0 : -1;
	}

	this.shortcuts = [{
		pattern     : 'ctrl+shift+n'
	},{
		pattern	: 'a',
		callback	:	function(){this.exec();}
	}];
}
